package com.cipta.lomob;

import org.json.JSONObject;

import tv.lomob.api.Lomob;
import tv.lomob.api.LomobResponseHandler;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cipta.lomob.base.BaseActivity;
import com.cipta.lomob.components.CropCircleTransformation;
import com.squareup.picasso.Picasso;

public class SignupActivity extends BaseActivity{
	private Button btnSignup;
	private ImageView profileImage;
	private TextView txtUsername, txtEmail;
	private EditText txtEditPassword;
	private JSONObject user;
	private ProgressDialog progressDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_signup);
		initializeViews();
		initializeProcess();
		initializeActions();
	}
	
	@Override
	public void initializeProcess() {
		super.initializeProcess();
		try{
			user = LomobManager.getInstance().getUser();
			txtUsername.setText("Full name : " + user.optString("firstname") + user.optString("lastname"));
			txtEmail.setText("Email : " + user.optString("username"));
			Picasso.with(this).load(user.optString("avatar")).placeholder(R.drawable.avatar_my_profile).transform(new CropCircleTransformation()).into(profileImage);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}



	@Override
	public void initializeViews() {
		// TODO Auto-generated method stub
		super.initializeViews();
		profileImage = (ImageView) findViewById(R.id.img_user);
		btnSignup = (Button)findViewById(R.id.btn_signup);
		txtUsername = (TextView) findViewById(R.id.txt_signup_fullname);
		txtEmail = (TextView)findViewById(R.id.txt_signup_email);
		txtEditPassword = (EditText) findViewById(R.id.edt_signup_password);
	}
	
	@Override
	public void initializeActions() {
		// TODO Auto-generated method stub
		super.initializeActions();
		btnSignup.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				ConnectFriendsActivity.toConnectFriendsActivity(SignupActivity.this);
				if (txtEditPassword.getText().equals("")) {
					showSignUpAlert();
				} else {
					proccessSignup();
				}
			}
		});
	}
	
	public static void toSignupActivity(Activity activity){
		activity.startActivity(new Intent(activity, SignupActivity.class));
	}
	
	private void proccessSignup(){
		String username = txtUsername.getText().toString();
		String password = txtEditPassword.getText().toString();
		
		if(username.isEmpty() && password.isEmpty()) return;
		
		//set password to object
		try{
			user.put("password", password);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		//process signup here
		progressDialog = new ProgressDialog(this);
		progressDialog.setCancelable(false);
		progressDialog.setMessage(getString(R.string.text_loading_signup_proccess));
		progressDialog.show();
		//link user to facebook
		Lomob.linkFacebook(user, new LomobResponseHandler() {
			
			@Override
			public void onSuccess(JSONObject result) {
				progressDialog.dismiss();
				try{
					String token = result.optString("token");
					user.put("token", token);
					LomobManager.getInstance().setUser(SignupActivity.this, user);
				}catch(Exception ex){
					ex.printStackTrace();
				}
				
				ConnectFriendsActivity.toConnectFriendsActivity(SignupActivity.this);
			}
			
			@Override
			public void onError(String message) {
				progressDialog.dismiss();
				AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
				builder.setMessage(message);
				builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						//for testing purpose
						ConnectFriendsActivity.toConnectFriendsActivity(SignupActivity.this);
					}
				});
				builder.show();	
			}
		});
	}
	
	private void showSignUpAlert(){
		final Dialog dialog = new Dialog(SignupActivity.this);
		dialog.setContentView(R.layout.dialog_signup_alert);
		Button btnOk = (Button)dialog.findViewById(R.id.btn_dialog_signup_);
		btnOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		dialog.show();
	}
}
